﻿using System.Collections.Generic;
using System.Xml;

namespace NBuild.Fx
{
    public sealed class XPathTaskItems : TaskItems<XmlNode>
    {
        private readonly string _xpath;
        private readonly XmlDocument _xdoc;

        public XPathTaskItems(string xpath)
        {
            _xpath = xpath;
        }

        public XPathTaskItems(string xpath, string xml) :
            this(xpath)
        {
            _xdoc = new XmlDocument();
            _xdoc.LoadXml(xml);
        }

        public override IEnumerator<XmlNode> GetEnumerator()
        {
            XmlNodeList nodes = _xdoc.SelectNodes(_xpath);
            if (nodes == null)
                yield break;
            foreach (XmlNode node in nodes)
                yield return node;
        }
    }
}
